package com.fk.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.fk.entity.Designer;
import com.fk.uitl.DBManager;

public class DesignerDaoImpl {
	public List<Designer> getDesignerList(){
		List<Designer> desList = new ArrayList<Designer>();
		com.fk.entity.Designer des = null;
		com.fk.entity.Designer designer = null;
		// 1:锟斤拷锟斤拷锟斤拷锟斤拷
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		// 2:锟斤拷锟斤拷Statement锟斤拷锟斤拷
		try {
			st  = conn.createStatement();
		// 3:sql锟斤拷锟�
			rs = st.executeQuery("select * from designer");
		// 锟斤拷锟斤拷
			while(rs.next()) {
				int designerId = rs.getInt("designerId");
				String name = rs.getString("name");
				String position = rs.getString("position");
				String img = rs.getString("img");
				des = new com.fk.entity.Designer(designerId, name, position, img);
				desList.add(des);
			}
			return desList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 鏍规嵁璁捐甯坕d鍒犻櫎
	 * rId瑕佸垹闄ょ殑璁捐甯坕d
	 * 鏁版嵁搴撳彈褰卞搷琛屾暟
	 * **/
	public int deleteRoleCateById(int rId) {
		return DBManager.executeUpdate("DELETE FROM designer WHERE designerid="+rId);
		
	}
	/**添加
	 * de
	 * int数据库返回受影响行数
	 * **/
	public int addDesigner(Designer de) {
		return DBManager.executeUpdate("insert into designer values(null,'"+de.getName()+"','"+de.getPosition()+"','"+de.getImg()+"')");
	}
	/**
	 * 通过分类名称查询产品分类
	 * 
	 * 
	 * **/
	public List<Designer> getDesignerByName(String name){
		List<Designer> deList = new ArrayList<Designer>();
		
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from designer where name='"+name+"'");
			while (rs.next()) {
				int deId = rs.getInt("designerId");
				String deName = rs.getString("name");
				String dePosition = rs.getString("position");
				String deImg = rs.getString("img");
				Designer de = new Designer(deId, deName, dePosition, deImg);
				deList.add(de);
				
			}
			System.out.println(deList);
			return deList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 编辑角色设计师
	 * @param did
	 * @return
	 */
	public Designer getDesignerById(int did) {
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		Designer d=null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("select * from designer where designerId="+did);
			while (rs.next()) {
				int designerId=rs.getInt(1);
				String name=rs.getString(2);
				String position=rs.getString(3);
				String img=rs.getString(4);
				d=new Designer(designerId, name, position, img);
			}
			return d;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	/**
	 * 修改角色设计师
	 * @param ud
	 * @return
	 */
	public int updateDesigner(Designer ud) {
		return DBManager.executeUpdate("update designer set name='"+ud.getName()+"',position='"+ud.getPosition()+"',img='"+ud.getImg()+"' where designerId="+ud.getDesignerId());
	}
}
